//
//  HDFeedbackViewController.swift
//  HuaDaoPeifu
//
//  Created by Erickson on 16/4/7.
//  Copyright © 2016年 paiyipai. All rights reserved.
//

import UIKit
import SVProgressHUD
class HDFeedbackViewController: UIViewController ,UITextFieldDelegate{
    let backView = UIView()
    let feedbackTextView = UITextView()
    let placeholderLabel = UILabel()
    let contactLabel = UILabel()
    let contactTextField = DKLoginFillPasswdTextField(placeholderText: "请留下您的手机号/QQ")
    let progress = HDProgressAlert()
    override var shouldAutorotate : Bool {
        return false
    }
    
    override func viewWillAppear(_ animated: Bool) {
        navigationController?.navigationBar.isHidden = false
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        feedbackTextView.delegate = self
        setupUI()
        setupAutoLaytou()
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        feedbackTextView.resignFirstResponder()
        contactTextField.resignFirstResponder()
    }
    
    fileprivate func setupUI() {
        title = "建议反馈"
        view.backgroundColor = UIColor.loginBackgroundColor
        let rightButton = UIBarButtonItem(title: "发送", style: UIBarButtonItemStyle.done, target: self, action: #selector(HDFeedbackViewController.sendFeedBack))
        navigationItem.rightBarButtonItem = rightButton
        
        placeholderLabel.config { (set: UILabel) -> Void in
            set.text = "在此输入您对我们的意见或者建议"
            set.textColor = UIColor(red:0.79, green:0.79, blue:0.81, alpha:1)
            set.font = .systemFont(ofSize: 16)
        }
        feedbackTextView.config { (set: UITextView) -> Void in
            set.isEditable = true
            set.font = .systemFont(ofSize: 16)
            
        }
        contactLabel.config { (set: UILabel) -> Void in
            set.font = UIFont.systemFont(ofSize: 16)
            set.text = "联系方式"
        }
        contactTextField.config { (set: UITextField) -> Void in
            set.isSecureTextEntry = false
            set.font = .systemFont(ofSize: 16)
            set.keyboardType = .numberPad
            if HDAccountHandler.loginType == .notLogin {
                set.text = nil
            }else {
                if let phone = HDAccountHandler.phone {
                    set.text = phone
                }
            }
            
        }
        
        backView.backgroundColor = UIColor.white
        backView.layer.borderWidth = 0.5
        backView.layer.borderColor = UIColor.cellSeparatorColor.cgColor
        backView.layer.masksToBounds = true
        backView.layer.cornerRadius = 2
        view.addSubview(backView)
        feedbackTextView.layer.masksToBounds = true
        feedbackTextView.layer.cornerRadius = 2
        view.addSubview(feedbackTextView)
        view.addSubview(contactLabel)
        view.addSubview(contactTextField)
        feedbackTextView.addSubview(placeholderLabel)
    }
    fileprivate func setupAutoLaytou() {
        
        backView.snp.makeConstraints { (make) -> Void in
            make.height.equalTo(160)
            make.top.equalTo(view.snp.top).offset(10)
            make.left.equalTo(view.snp.left).offset(15)
            make.right.equalTo(view.snp.right).offset(-15)
        }
        
        feedbackTextView.snp.makeConstraints { (make) -> Void in
            make.height.equalTo(158)
            make.top.equalTo(view.snp.top).offset(11)
            make.left.equalTo(view.snp.left).offset(20)
            make.right.equalTo(view.snp.right).offset(-16)
        }
        placeholderLabel.snp.makeConstraints { (make) -> Void in
            make.left.equalTo(feedbackTextView.snp.left).offset(5)
            make.top.equalTo(feedbackTextView.snp.top).offset(10)
            make.height.equalTo(16)
            
        }
        contactLabel.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(feedbackTextView.snp.bottom).offset(10)
            make.left.equalTo(view.snp.left).offset(16)
            
        }
        contactTextField.snp.makeConstraints { (make) -> Void in
            make.height.equalTo(40)
            make.top.equalTo(contactLabel.snp.bottom).offset(10)
            make.left.equalTo(view.snp.left).offset(15)
            make.right.equalTo(view.snp.right).offset(-15)
            
        }
        
    }
    func sendFeedBack() {
        
        
        if feedbackTextView.text.characters.count == 0 || feedbackTextView.text == nil {
            SVProgressHUD.showError("请输入内容")
            return
        }
        var feedbackModel = PYPFeedback()
        feedbackModel.content = feedbackTextView.text
        debugPrint(HDAccountHandler.phone)
        if HDAccountHandler.loginType == .notLogin {
            if let address = contactTextField.text {
                feedbackModel.phone = address
            } else {
                feedbackModel.phone = ""
            }
            feedbackModel.atk = ""
        } else {
            feedbackModel.atk = HDAccountHandler.token
            
            if let address = contactTextField.text {
                feedbackModel.phone = address
            } else {
                feedbackModel.phone = HDAccountHandler.phone
            }
        }
        
        
        HDApiManagerHandler.requestFeedback(feedbackModel, success: { (dict) -> () in
            debugPrint(dict["msg"])
            if dict["status"]?.int32Value == 1 {
                SVProgressHUD.showSuccess(dict["msg"] as! String)
                self.navigationController?.popToRootViewController(animated: true)
            } else {
                
                SVProgressHUD.showError(dict["msg"] as! String)
            }
        }) { (error) -> () in
        }
    }
}

extension HDFeedbackViewController: UITextViewDelegate {
    
    func textViewDidChange(_ textView: UITextView) {
        if textView.text == "" {
            placeholderLabel.isHidden = false
        } else {
            placeholderLabel.isHidden = true
        }
    }
}

class DKLoginFillPasswdTextField: UITextField {
    
    init(placeholderText: String) {
        super.init(frame: CGRect.zero)
        placeholder = placeholderText
        
        leftView = UIView(frame: CGRect(x: 0,y: 0,width: 10,height: 0))
        leftViewMode = .always
        backgroundColor = .white
        layer.borderWidth = 0.5
        layer.borderColor = UIColor.cellSeparatorColor.cgColor
        isSecureTextEntry = true
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
}

